package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cc.comac.mom.util.user.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityTestContent;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.QualityTestContentDao;
import cc.comac.mom.entity.QualityTestContentEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
public class QualityTestContentDataHandler extends ServiceImpl<QualityTestContentDao, QualityTestContentEntity> {

	@Autowired
	private UserUtil userUtil;
	@Autowired
	private QualityTestContentDao qualityTestContentDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public ResultBean insert(QualityTestContent obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null || obj.getInnerData() == null) {
			resultBean.setErrorMessge("数据为空");
			return resultBean;
		}

		if (obj.getInnerData().getTestNo() == null || "".equals(obj.getInnerData().getTestNo())) {
			resultBean.setErrorMessge("实验编号不能为空");
			return resultBean;
		}
		obj.getInnerData().setTestUser(userUtil.getUserInfoByEcode(obj.getInnerData().getTestUserJobNumber()).getTruename());
		baseEntityDataUtil.setBaseEntityData(obj.getInnerData());
		int insert = qualityTestContentDao.insert(obj.getInnerData());
		if (insert < 1) {
			resultBean.setErrorMessge("实验内容插入失败");
			return resultBean;
		}
		return resultBean;

	}

	public LimitPageBean<QualityTestContentEntity> selectByCondition(QualityTestContent obj) {
		QueryWrapper<QualityTestContentEntity> queryWrapper = new QueryWrapper<QualityTestContentEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getTestNo() != null && !"".equals(obj.getInnerData().getTestNo())) {
				queryWrapper.like("test_no", obj.getInnerData().getTestNo());
			}
			if (obj.getInnerData().getTestName() != null && !"".equals(obj.getInnerData().getTestName())) {
				queryWrapper.like("test_name", obj.getInnerData().getTestName());
			}
			if (obj.getInnerData().getTestUser() != null && !"".equals(obj.getInnerData().getTestUser())) {
				queryWrapper.like("test_user", obj.getInnerData().getTestUser());
			}

		}
		Page<QualityTestContentEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<QualityTestContentEntity> iPage = qualityTestContentDao.selectPage(page, queryWrapper);
		@SuppressWarnings("unchecked")
		LimitPageBean<QualityTestContentEntity> limitPageBean = (LimitPageBean<QualityTestContentEntity>) LimitPageBean
				.of(iPage);
		return limitPageBean;

	}

	/**
	 * 通过实验编号获取实验内容
	 */

	public List<QualityTestContent> getByTestNo(String testNo) {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("test_no", testNo);
		List<QualityTestContentEntity> selectByMap = qualityTestContentDao.selectByMap(map);
		if (selectByMap == null || selectByMap.size() < 1) {
			return null;
		}
		ArrayList<QualityTestContent> list = new ArrayList<QualityTestContent>();

		for (QualityTestContentEntity item : selectByMap) {
			QualityTestContent qualityTestContent = new QualityTestContent();
			qualityTestContent.setInnerData(item);
			list.add(qualityTestContent);
		}

		return list;

	}
}
